﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Persistence = Entersoft.Data.DotNetDAL.SQL.Persistence;

namespace BLL
{
    public class Modulos: Common
    {
        public static int ObtenerMaxId()
        {
            var retorno = 0;

            var objRP = new Entersoft.Data.DotNetResponse.SQL.Persistence { StoredProcedure = "SP_ObtMax_ID2" };
            objRP.AddParametro("TBL_Modulos");
            objRP.AddParametro("MODULO_ID");

            using (var objConn = new Persistence(false, ConnDB))
            {
                objConn.ExecuteDataTable(ref objRP);

                if (!objRP.TieneErrores)
                    retorno = (int)objRP.DataTable.Rows[0]["Val_Max"];
            }

            return retorno;
        }

        public static IList<Modulo> ObtenerModulos()
        {
            var retorno = new List<Modulo>();

            var objRP = new Entersoft.Data.DotNetResponse.SQL.Persistence { StoredProcedure = "sp_get_Modulos" };

            using (var objConn = new Persistence(false, ConnDB))
            {
                objConn.ExecuteDataTable(ref objRP);

                if (!objRP.TieneErrores)
                {
                    foreach(DataRow row in objRP.DataTable.Rows)
                    {
                        retorno.Add(new Modulo(Convert.ToInt32(row["MODULO_ID"]), row["GLOSA"].ToString(), row["URL"].ToString(), Convert.ToInt32(row["MODULO_PADRE"]), Convert.ToInt32(row["POSICION"]), Convert.ToInt32(row["PERFIL"])));
                    }
                    
                }
            }

            return retorno;
        }

        public static IList<Perfil> ObtenerPerfiles()
        {
            return Perfiles.ObtenerPerfiles();
        } 
    }

    public class Modulo
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        private int mModuloId;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private string mGlosa;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private string mURL;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private int mModuloPadre;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private int mPosicion;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private readonly int mPerfilId;
            
        public Modulo(int ModuloId, string Glosa, string URL, int ModuloPadre, int Posicion, int PerfilId)
        {
            mModuloId = ModuloId;
            mGlosa = Glosa;
            mURL = URL;
            mModuloPadre = ModuloPadre;
            mPosicion = Posicion;
            mPerfilId = PerfilId;
        }

        public int ModuloId
        {
            get { return mModuloId; }
            set { mModuloId = value; }
        }
            
        public string Glosa
        {
            get { return mGlosa; }
            set { mGlosa = value; }
        }
            
        public string URL
        {
            get { return mURL; }
            set { mURL = value; }
        }

        public int ModuloPadre
        {
            get { return mModuloPadre; }
            set { mModuloPadre = value; }
        }

        public int Posicion
        {
            get { return mPosicion; }
            set { mPosicion = value; }
        }

        public string Perfil
        {
            get
            {
                Perfil retorno = null;

                var tmpPerfiles = Perfiles.ObtenerPerfiles();

                foreach (var tmpPerfil in tmpPerfiles)
                {
                    if (tmpPerfil.PerfilId != mPerfilId) continue;

                    retorno = tmpPerfil;
                    break;
                }

                return retorno != null ? retorno.Glosa : null;
            }
        } 

    }
}
